const { getLogs, getAddress } = require('../helper/cache/getLogs')
const { staking } = require("../helper/staking");

const stakingContract = "0x6413707acd0eF29E54e4f7eE931bb00575868eA4";
const PINKAV = "0xE5274E38E91b615D8822e8512a29A16FF1B9C4Af";
const config = {
  kava: { lendingPool: "0x11C3D91259b1c2Bd804344355C6A255001F7Ba1e", fromBlock: 5281411, },
};

async function getTokens(api) {
  const { lendingPool, fromBlock } = config[api.chain]
  const logs = await getLogs({
    api,
    target: lendingPool,
    topics: ['0x9f8f649e3f624ae845ed20c597f2841f852ba62903a53736c2b36d67869ca919'],
    fromBlock,
  })
  return logs.map(log => getAddress(log.topics[1]))
}

async function tvl(api) {
  const { lendingPool } = config[api.chain]
  const tokens = await getTokens(api)
  return api.sumTokens({ tokens, owners: [lendingPool] })
}

async function borrowed() {
  return {}
}

module.exports = {
  methodology:
    "Counts the total tokens supplied to the lending pool and the total tokens borrowed from the lending pool.",
  kava: {
    staking: staking(stakingContract, PINKAV),
  },
  hallmarks: [
    [1688670115, "Multichain Exploit"],
  ],
  deadFrom: '2025-12-05',
};


Object.keys(config).forEach(chain => {
  module.exports[chain] = {
    ...(module.exports[chain] || {}),
    tvl, borrowed,
  }
})